//
//  CarouselView.m
//  Leisure
//
//  Created by lanou on 16/4/21.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ScrollCarouselView.h"
#import "UIImageView+WebCache.h"

@interface ScrollCarouselView ()<UIScrollViewDelegate>

// 最下面的scrollView
@property (nonatomic, strong) UIScrollView *scrollView;
// 网址数组
@property (nonatomic, strong) NSArray *imageUrls;

@property (nonatomic, strong) UIPageControl *page;

// 定时器 当用户拖拽的时候 不需要自动滑动 所以设置为属性
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ScrollCarouselView

- (instancetype)initWithFrame:(CGRect)frame imageUrls:(NSArray *)imageUrls
{
    self = [super initWithFrame:frame];
    if (self) {
        if (imageUrls.count == 0) {
            return nil;
        }
        self.imageUrls = imageUrls;
        [self createScrollView];
        [self initImages];
        [self createPageController];
        // 两张图以上才创建timer
        if (self.imageUrls.count >= 2) {
            [self createTimer];
        }
    }
    return self;
}

// 添加分页控制器
- (void)createPageController
{
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width - 100, self.frame.size.height - 20 , 100, 20)];
//    self.page.backgroundColor = [UIColor redColor];
    [self addSubview:self.page];
    // 一共有几个点点
    self.page.numberOfPages = self.imageUrls.count;
}

- (void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width * self.imageUrls.count, 0);
    self.scrollView.backgroundColor = [UIColor clearColor];
//    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self addSubview:self.scrollView];
}

- (void)initImages
{
    // 如果只有一个网址(一张图片)不用循环播放 直接创建一张imageView;
    if (self.imageUrls.count == 1) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[0]]];
        [self.scrollView addSubview:imageView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFuncation)];
        [imageView addGestureRecognizer:tap];
        imageView.userInteractionEnabled = YES;
        return;
    }
    // 在左右两边各添加一张imageView
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width * (self.imageUrls.count + 2), 0);
    // 整页滚动
    self.scrollView.pagingEnabled = YES;
    // 一上来就显示第一个
    self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    for (int i = 0; i < self.imageUrls.count + 2; i++) {
        // 点击手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFuncation)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        // 给每一张图片都添加一个点击事件
        [imageView addGestureRecognizer:tap];
        
        // 打开交互
        imageView.userInteractionEnabled = YES;
        
        // 第一张显示最后一张的内容
        if (i == 0) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls.lastObject]];
            // 最后一张显示第一张得内容
        } else if (i == self.imageUrls.count + 1) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls.firstObject]];
        } else {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrls[i - 1]]];
        }
        [self.scrollView addSubview:imageView];
    }
}

// 图片点击
- (void)imageClickFuncation
{
    
    if (self.imageClick) {
        self.imageClick(self.page.currentPage);
    }
}

// 结束减速时判断偏移量
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 如果偏移到第0个 回到最后一个
    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(self.imageUrls.count * self.frame.size.width, 0);
    }
    // 如果偏移到最后一个 回到第一个
    if (self.scrollView.contentOffset.x == self.frame.size.width * (self.imageUrls.count + 1)) {
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    self.page.currentPage = self.scrollView.contentOffset.x / self.frame.size.width - 1;
}

// 正在滚动中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 如果偏移到第0个 回到最后一个
    if (self.scrollView.contentOffset.x <= 0) {
        self.scrollView.contentOffset = CGPointMake(self.imageUrls.count * self.frame.size.width, 0);
    }
    // 如果偏移到最后一个 回到第一个
    if (self.scrollView.contentOffset.x >= self.frame.size.width * (self.imageUrls.count + 1)) {
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
}

// 当我们代码更改了偏移量 并且使用了动画的时候 会调用这个方法 不会调用结束减速的方法 我们可以手动调用
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollViewDidEndDecelerating:self.scrollView];
}

- (void)createTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(timerHander) userInfo:nil repeats:YES];
}

// 开始拖拽时继续定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 定时器没有提供暂停的方法 我们可以设置它在什么时间开启
    [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:MAXFLOAT]];
}

// 结束拖拽时继续定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:interval]];
}

// 定时器方法
- (void)timerHander
{
    CGPoint offSet = self.scrollView.contentOffset;
    offSet.x += self.frame.size.width;
    [self.scrollView setContentOffset:offSet animated:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
